const express = require('express');
const { uploadMiddleWare, uploadErr } = require('../middleware/fileMiddleware');

const router = express.Router();

router.post('/upload', (req, res) => {
    uploadMiddleWare(req, res, (err) => {
        // 处理错误
        if(err){
            console.log('错误', err);
            let msg = '';
            for (const key in uploadErr) {
                if( String(err).indexOf(key) >=0 ){
                    msg = uploadErr[key];
                }
            }
            res.send({
                code: 500,
                data: null,
                msg,
            })
        }else{
            const { file } = req;
            console.log(file);
            res.send({
                code: 200,
                data: {
                    name: file.filename,
                    path: file.path,
                    size: file.size,
                },
                msg: "上传成功",
            })
        }
    })
    
})

module.exports = router;